
Object.defineProperty(exports, "__esModule", { value: true });
/**
* @module pub
* @author: Hjava
* @description: Pub模块
* @since: 2018-07-22 16:15:34
*/
var eventEmitter_1 = __webpack_require__(/*! _util/eventEmitter */ "./src/core/utils/eventEmitter.ts");
var pub_constant_1 = __webpack_require__(/*! _pub/pub.constant */ "./src/core/pub/pub.constant.ts");
var pub_session_1 = __webpack_require__(/*! _pub/pub.session */ "./src/core/pub/pub.session.ts");
var pub_message_1 = __webpack_require__(/*! _pub/pub.message */ "./src/core/pub/pub.message.ts");
var analytics_1 = __webpack_require__(/*! _util/analytics */ "./src/core/utils/analytics.ts");
var constant_1 = __webpack_require__(/*! _util/constant */ "./src/core/utils/constant.ts");
var owlController_1 = __webpack_require__(/*! _util/owlController */ "./src/core/utils/owlController.ts");
var log_1 = __webpack_require__(/*! ./../utils/log */ "./src/core/utils/log.ts");
var ACK_PUB = [pub_session_1.default.PubAckMsgs, pub_session_1.default.PubAckSyncMsgs];
var PubSDK = /** @class */ (function () {
    function PubSDK(loginSDK) {
        var _this = this;
        this._loginSDK = null;
        this._timeMap = {};
        this._sendMessageTimeoutMap = {};
        this._readTimeMap = {};
        this.constant = pub_constant_1.default;
        this._loginSDK = loginSDK;
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SEND_MSG_REQ, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SEND_MSG_REQ2, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SEND_MSG_RES, this._onACK.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_CANCEL_MSG, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_CANCEL_MSG2, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_CANCEL_MSG_RES, this._onACK.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SEND_MSG_KF_REQ, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SEND_MSG_KF_REQ2, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SEND_MSG_KF_RES, this._onACK.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_CANCEL_KF_MSG, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_CANCEL_KF_MSG2, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_CANCEL_KF_MSG_RES, this._onACK.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SEND_TT_REQ, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SEND_TT_RES, this._onACK.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_BC_MSG_REQ, this._onMessage.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SYNC_READ, this._onRead.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SYNC_READ2, this._onRead.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_SYNC_READ_RES, this._onReadACK.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_MARK_READ, this._onNotice.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_MARK_READS, this._onNotice.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_OPPOSITE_READ, this._onOppositeRead.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_OPPOSITE_READ2, this._onOppositeRead.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_OPPOSITE_READ_RES, this._onOppositeReadACK.bind(this));
        eventEmitter_1.default.on(pub_constant_1.default.PUB.URI_PUB_ADDITION_MSG2, this._onMessageAddition.bind(this));
        /**
          * batch ack 公众号的离线接口会触发这个，不过目前离线接口已经不再使用了
         */
        eventEmitter_1.default.on('__PubAck', function (msgs) {
            var AA = [
                [],
                []
            ];
            for (var i = 0; i < msgs.length; i++) {
                AA[1 & (msgs[i].fromUid == _this._loginSDK.getUID())]
                    .push(msgs[i].msgId);
            }
            var Id = 0;
            for (var j = 0; j < 2; j++) {
                while ((Id = AA[j].splice(0, 1500)).length) {
                    _this.send(new ACK_PUB[j]({
                        uid: _this._loginSDK.getUID(),
                        msgIds: Id
                    }));
                }
            }
        });
        // Pub单聊和Pub客服都是使用同一个ACK。感觉是废弃了，没有找到发送通知的地方 
        eventEmitter_1.default.on('pubAcks', (function (mids) {
            if (mids.length > 0) {
                this.send(new pub_session_1.default.PubAckMsgs({
                    uid: this._loginSDK.getUID(),
                    msgIds: mids
                }));
            }
        }).bind(this));
    }
    PubSDK.prototype.send = function (message) {
        try {
            this._loginSDK._sendTransUp(pub_constant_1.default.SVID_PUB, message);
            if (message.uri === pub_constant_1.default.PUB.URI_PUB_SEND_MSG_REQ
                || message.uri === pub_constant_1.default.PUB.URI_PUB_SEND_MSG_REQ2
                || message.uri === pub_constant_1.default.PUB.URI_PUB_CANCEL_MSG
                || message.uri === pub_constant_1.default.PUB.URI_PUB_CANCEL_MSG2
                || message.uri === pub_constant_1.default.PUB.URI_PUB_CANCEL_KF_MSG
                || message.uri === pub_constant_1.default.PUB.URI_PUB_CANCEL_KF_MSG2
                || message.uri === pub_constant_1.default.PUB.URI_PUB_SEND_MSG_KF_REQ
                || message.uri === pub_constant_1.default.PUB.URI_PUB_SEND_MSG_KF_REQ2
                || message.uri === pub_constant_1.default.PUB.URI_PUB_SEND_TT_REQ
                || message.uri === pub_constant_1.default.PUB.URI_PUB_BC_MSG_REQ) {
                var valObject = {
                    id: message.msgUuid,
                    chat: message.toUid ? message.toUid : (message.pubUid ? message.pubUid + '_' + message.receiverUid : void 0),
                    messageType: message.type ? message.type : 0,
                    sessionType: message.uri === (pub_constant_1.default.PUB.URI_PUB_SEND_MSG_REQ
                        || message.uri === pub_constant_1.default.PUB.URI_PUB_CANCEL_MSG
                        || pub_constant_1.default.PUB.URI_PUB_SEND_MSG_REQ2
                        || pub_constant_1.default.PUB.URI_PUB_CANCEL_MSG2) ? 'pubchat' : 'cschat'
                };
                // Log.log('IMSDK::send pub message info:' + JSON.stringify(valObject))
                analytics_1.default.report({
                    nm: 'message_send_start',
                    val: valObject
                });
                this._timeMap[message.msgUuid] = Date.now();
            }
            if (message.uri === pub_constant_1.default.PUB.URI_PUB_SYNC_READ || message.uri === pub_constant_1.default.PUB.URI_PUB_SYNC_READ2) {
                var web_read_object = JSON.stringify(message.syncReadItems.map(function (item) {
                    return {
                        id: item.chatId,
                        peerUid: item.peerUid,
                        cts: item.stamp,
                        sts: item.sts
                    };
                }));
                // Log.log('IMSDK::send pub send read sync info:' + web_read_object)
                analytics_1.default.report({
                    nm: 'read_pub_send',
                    val: {
                        uuid: message.msgUuid,
                        'web_read': web_read_object
                    }
                });
                if (message.msgUuid && !this._readTimeMap[message.msgUuid]) {
                    this._readTimeMap[message.msgUuid] = Date.now();
                }
                this._sendMessageTimeoutMap[message.msgUuid] = setTimeout(function () {
                    owlController_1.default.send({
                        name: 'message_send_finished',
                        value: 0,
                        tag: {
                            status: 0
                        }
                    });
                }, 15 * 1000);
            }
        }
        catch (e) {
            log_1.default.error(e);
        }
    };
    /**
     * @param message {Message}
     */
    PubSDK.prototype._onMessage = function (message) {
        try {
            this._ack(message, this._loginSDK.getUID());
            // let valObject = {
            //     id: message.msgUuid,
            //     mid: typeof message.msgId === 'string' ? message.msgId : message.msgId.toString(),
            //     chat: message.toUid ? (userData.get('uid') === message.toUid ? message.fromUid : message.toUid) : (message.pubUid ? (userData.get('uid') === message.receiverUid ? message.pubUid + '_' + message.fromUid : message.pubUid + '_' + message.receiverUid) : void 0),
            //     messageType: message.type ? message.type : 0
            // }
            // Log.log('IMSDK::_onMessage pub receive online message info:' + JSON.stringify(valObject))
            // logManager.report({
            //     nm: 'message_receive_success',
            //     val: valObject
            // });
            // owlController.send({
            //     name: 'message_receive_success',
            //     value: 0
            // });
        }
        catch (e) {
            log_1.default.error(e);
        }
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.MESSAGE, message);
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.PUB_MESSAGE, message);
    };
    PubSDK.prototype._ack = function (message, uid) {
        try {
            this.send(new ACK_PUB[1 & (message.fromUid == uid)]({
                uid: uid,
                msgIds: [message['msgId']]
            }));
        }
        catch (e) {
            log_1.default.error(e);
        }
    };
    /**
     * @param message {Message}
     */
    PubSDK.prototype._onRead = function (message) {
        var web_read_object = JSON.stringify(message.syncReadItems.map(function (item) {
            return {
                id: item.chatId,
                peerUid: item.peerUid,
                cts: item.stamp,
                sts: item.sts
            };
        }));
        // Log.log('IMSDK::_onRead pub receive read sync info:' + web_read_object)
        analytics_1.default.report({
            nm: 'read_pub_receive',
            val: {
                'web_read': web_read_object
            }
        });
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.PUB_READ, message);
        eventEmitter_1.default.emit('read', message);
    };
    PubSDK.prototype._onReadACK = function (_a) {
        var msgUuid = _a.msgUuid;
        if (msgUuid && this._readTimeMap[msgUuid]) {
            var valObject = {
                uuid: msgUuid,
                time: Date.now() - this._readTimeMap[msgUuid]
            };
            // Log.log('IMSDK::_onReadACK pub send read sync result info:' + JSON.stringify(valObject))
            analytics_1.default.report({
                nm: 'read_pub_success',
                val: valObject
            });
            delete this._readTimeMap[msgUuid];
        }
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.PUB_READ_ACK, { uuid: msgUuid });
    };
    /**
     * @param message {Message}
     */
    PubSDK.prototype._onACK = function (message) {
        if (message.rescode === 0 && this._timeMap[message.msgUuid]) {
            var valObject = {
                id: message.msgUuid,
                mid: message.msgId,
                time: Date.now() - this._timeMap[message.msgUuid]
            };
            // Log.log('IMSDK::send pub message success info:' + JSON.stringify(valObject))
            analytics_1.default.report({
                nm: 'message_send_success',
                val: valObject
            });
            owlController_1.default.send({
                name: 'message_send_finished',
                value: Date.now() - this._timeMap[message.msgUuid],
                tag: {
                    status: 1
                }
            });
            clearTimeout(this._sendMessageTimeoutMap[message.msgUuid]);
            delete this._sendMessageTimeoutMap[message.msgUuid];
        }
        else {
            var valObject = {
                code: message.rescode,
                id: message.msgUuid
            };
            // Log.log('IMSDK::send pub message fail info:' + JSON.stringify(valObject))
            analytics_1.default.report({
                nm: 'message_send_error',
                val: valObject
            });
        }
        // 重置
        this._timeMap[message.msgUuid] = void 0;
        eventEmitter_1.default.emit('ack', message);
    };
    /**
     * @param message {!IMGroupNotice}
     */
    PubSDK.prototype._onNotice = function (message) {
        eventEmitter_1.default.emit('notice', message);
    };
    PubSDK.prototype._onOppositeRead = function (message) {
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.PUB_OPPOSITE_READ, message);
    };
    PubSDK.prototype._onOppositeReadACK = function (ack) {
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.PUB_OPPOSITE_READ_ACK, ack);
    };
    PubSDK.prototype._onMessageAddition = function (data) {
        eventEmitter_1.default.emit(constant_1.MESSAGE_EVENT.PUB_MESSAGE_ADDITION, data);
    };
    PubSDK.constant = pub_constant_1.default;
    PubSDK.Session = pub_session_1.default;
    PubSDK.Message = pub_message_1.default;
    return PubSDK;
}());
exports.default = PubSDK;


//# sourceURL=webpack://MTDX/./src/core/pub/index.ts?